package cn.com.fuhuitong.main.order.adapter

import android.content.Context
import android.view.View
import cn.com.fuhuitong.R
import cn.com.fuhuitong.app.BASE_HTTP_URL
import cn.com.fuhuitong.app.getImageUrl
import cn.com.fuhuitong.base.BaseDelegateAdapter
import cn.com.fuhuitong.main.ItemViewType
import cn.com.fuhuitong.main.order.entity.OrderDetailsEntity
import cn.com.fuhuitong.main.order.entity.OrderGoodsListEntity
import cn.com.fuhuitong.utils.SpanUtils
import com.alibaba.android.vlayout.LayoutHelper
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.TimeUtils
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.item_order_details_address.view.*
import kotlinx.android.synthetic.main.item_order_details_goods.view.*
import kotlinx.android.synthetic.main.item_order_details_goods_heads.view.*
import kotlinx.android.synthetic.main.item_order_details_nature.view.*
import kotlinx.android.synthetic.main.item_order_details_other.view.*
import kotlinx.android.synthetic.main.item_order_details_state.view.*

/**
 * 订单详情状态适配器
 * */
class OrderDetailsStateAdapter(
    private val mContext: Context, private val mHelper: LayoutHelper
) : BaseDelegateAdapter<OrderDetailsEntity>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_order_details_state,
    mItemViewType = ItemViewType.ORDER_DETAILS_STATE
) {
    init {
        addChildClickViewIds(
            R.id.text_order_details_cancel,
            R.id.text_order_details_pay,
            R.id.text_order_details_take
        )
    }

    override fun onBindViewHolder(itemView: View, position: Int, itemData: OrderDetailsEntity) {
        super.onBindViewHolder(itemView, position, itemData)
        itemView.text_order_details_status.text = when (itemData.status) {
            1 -> "等待支付"
            2 -> "已发货"
            3 -> "订单已完成"
            else -> "订单已取消"
        }
        itemView.text_order_details_describe.text = when (itemData.status) {
            3 -> "感谢您对双缘健康的信任，期待再次光临"
            4 -> "8分钟内未支付的订单将自动取消"
            else -> ""
        }
        itemView.text_order_details_describe.visibility = when (itemData.status) {
            1, 2 -> View.GONE
            else -> View.VISIBLE
        }
        itemView.linear_order_details_btn.visibility = when (itemData.status) {
            1 -> View.VISIBLE
            else -> View.GONE
        }
        itemView.text_order_details_take.visibility = when (itemData.status) {
            2 -> View.VISIBLE
            else -> View.GONE
        }
    }
}

/**
 * 订单详情物品头部适配器
 * */
class OrderDetailsGoodsHeadsAdapter(
    private val mContext: Context, private val mHelper: LayoutHelper
) : BaseDelegateAdapter<OrderDetailsEntity>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_order_details_goods_heads,
    mItemViewType = ItemViewType.ORDER_DETAILS_GOODS_HEADS
) {
    override fun onBindViewHolder(itemView: View, position: Int, itemData: OrderDetailsEntity) {
        super.onBindViewHolder(itemView, position, itemData)
        var count = 0
        itemData.goods_list.forEach {
            count += it.num
        }
        itemView.text_order_details_number.text = "共${count}件"
    }
}

/**
 * 订单详情物品适配器
 * */
class OrderDetailsGoodsAdapter(
    private val mContext: Context, private val mHelper: LayoutHelper
) : BaseDelegateAdapter<OrderGoodsListEntity>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_order_details_goods,
    mItemViewType = ItemViewType.ORDER_DETAILS_GOODS
) {
    override fun onBindViewHolder(itemView: View, position: Int, itemData: OrderGoodsListEntity) {
        super.onBindViewHolder(itemView, position, itemData)

        Glide.with(itemView).load(getImageUrl(itemData.image))
            .into(itemView.image_order_details_goods_icon)
        itemView.text_order_details_goods_name.text = itemData.goods_title
        itemView.text_order_details_goods_price.text =
            SpanUtils.decimalFormat(itemData.price.toDouble())
        itemView.text_order_details_goods_count.text = "x${itemData.num}"

    }
}

/**
 * 订单详情积分抵扣适配器
 * */
class OrderDetailsNatureAdapter(
    private val mContext: Context, private val mHelper: LayoutHelper
) : BaseDelegateAdapter<OrderDetailsEntity>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_order_details_nature,
    mItemViewType = ItemViewType.ORDER_DETAILS_NATURE
) {
    override fun onBindViewHolder(itemView: View, position: Int, itemData: OrderDetailsEntity) {
        super.onBindViewHolder(itemView, position, itemData)
        itemView.text_order_details_total.text =
            SpanUtils.decimalFormat(itemData.goods_total_price.toDouble())
        itemView.text_order_details_deduction.text =
            SpanUtils.decimalFormat(itemData.score_price.toDouble())
        itemView.text_order_details_freight.text =
            SpanUtils.decimalFormat(itemData.delivery_fee.toDouble())
    }
}

/**
 * 订单详情地址适配器
 * */
class OrderDetailsAddressAdapter(
    private val mContext: Context, private val mHelper: LayoutHelper
) : BaseDelegateAdapter<OrderDetailsEntity>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_order_details_address,
    mItemViewType = ItemViewType.ORDER_DETAILS_ADDRESS
) {
    override fun onBindViewHolder(itemView: View, position: Int, itemData: OrderDetailsEntity) {
        super.onBindViewHolder(itemView, position, itemData)
        itemView.text_order_details_consignee.text = itemData.consignee
        itemView.text_order_details_phone.text = itemData.mobile
        itemView.text_order_details_address.text = itemData.address
    }
}

/**
 * 订单详情其他适配器
 * */
class OrderDetailsOtherAdapter(
    private val mContext: Context, private val mHelper: LayoutHelper
) : BaseDelegateAdapter<OrderDetailsEntity>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_order_details_other,
    mItemViewType = ItemViewType.ORDER_DETAILS_OTHER
) {
    override fun onBindViewHolder(itemView: View, position: Int, itemData: OrderDetailsEntity) {
        super.onBindViewHolder(itemView, position, itemData)
        itemView.text_order_details_sn.text =
            if (itemData.order_sn.isNotEmpty())
                itemData.order_sn else ""
        itemView.text_order_details_remarks.text =
            if (itemData.remarks.isNotEmpty())
                itemData.remarks else ""
        itemView.text_order_details_pay_mode.text =
            when (itemData.pay_type) {
                1 -> "微信支付"
                2 -> "支付宝支付"
                else -> ""
            }
        itemView.text_order_details_place_time.text =
            if (itemData.createtime != 0L)
                TimeUtils.millis2String(itemData.createtime * 1000, "yyyy-MM-dd HH:mm") else ""
        itemView.text_order_details_pay_time.text =
            if (itemData.pay_time != 0L)
                TimeUtils.millis2String(itemData.pay_time * 1000, "yyyy-MM-dd HH:mm") else ""
    }
}

